<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <h1>用户注册</h1>
        用户名：<input type="text" id="username" /> 密 码：<input
            type="password"
            id="pwd"
        />
        email: <input type="email" id="email" />
        <button id="registerbtn">提 交</button>
    </body>
    <script>
        window.onload = function () {
            registerbtn.onclick = register
        }
        function register() {
            let user = username.value,
                pwds = pwd.value,
                emails = email.value
            if (!user || !pwds || !emails) {
                alert('用户名或密码或邮箱不能为空')
                return false
            }
            let xhr = new XMLHttpRequest()
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    let res = JSON.parse(xhr.responseText)
                    if (res.code == -2) {
                        alert(res.msg)
                    }
                    if (res.code == 200) {
                        alert('注册成功')
                        window.location.href =
                            'login.html?username=' + user + '&password=' + pwds
                    }
                }
            }
            xhr.open('post', 'https://liu.zzgoodqc.cn/register', true)
            xhr.setRequestHeader(
                'Content-Type',
                'application/x-www-form-urlencoded'
            )
            let data =
                'username=' + user + '&password=' + pwds + '&email=' + emails
            xhr.send(data)
        }
    </script>
</html>
